<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/8/29
 * Time: 下午6:42
 */
namespace app\common\model;

use think\Model;
use think\facade\Cache;

class ArticleCategoryModel extends Model
{
    const SHOW_STATUS=1; //显示
    const HIDE_STATUS=0; //不显示

    protected $autoWriteTimestamp = true;

    protected $pk='cat_id';

    /**
     * 所有分类
     * @param $update //更新，1 更新，0 不更新
     * @return mixed
     */
    public static function getALL($update=0)
    {
        $model = new static;
        if (!Cache::get('article_category') || $update) {
            $field="cat_id,cat_name,cat_key,status,list_order,parent_id,cat_alias";
            $data = $model->field($field)->order(['list_order' => 'asc', 'create_time' => 'asc'])->select();
            $all = !empty($data) ? $data->toArray() : [];
            $tree = [];
            foreach ($all as $first) {
                if($first["parent_id"]!=0) continue;

                $first['child'] = self::getAllCat($all,$first['cat_id']);;

                $tree[]=$first;
            }
            Cache::tag('cache')->set('article_category', compact('all','tree'));
        }
        return Cache::get('article_category');
    }

    /**
     * 所有子分类
     * @param $all
     * @param $id
     * @return array
     */
    private static function getAllCat($all,$id){
        $arr=[];

        foreach ($all as $vo){
            if($vo["parent_id"]!=$id) continue;

            $vo['child'] = self::getAllCat($all,$vo['cat_id']);

            $arr[]=$vo;
        }

        return $arr;
    }

    /**
     * 增加分类
     * @param $data
     * @return bool
     */
    public function adds($data){
        //检测当前应用方法名是否重复
        if(!self::chackAction($data)){
            return['status'=>0,'msg'=>'标识已存在！'];
        }

        if(empty($data['cat_id'])){
            $status = self::save($data);

            if($status){
                self::getALL(1);
                return['status'=>1,'msg'=>'增加成功！'];
            }else{
                return['status'=>0,'msg'=>'增加失败！'];
            }
        }else{
            $status = self::save($data,['cat_id'=>$data["cat_id"]]);

            if($status){
                self::getALL(1);
                return['status'=>1,'msg'=>'修改成功！'];
            }else{
                return['status'=>0,'msg'=>'修改失败！'];
            }
        }

    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id){
        $count=self::where('parent_id',$id)
            ->count();

        if($count>0){
            return ['status'=>0,'msg'=>'有子栏目不能删除！'];
        }

        $status=self::destroy($id);
        if($status){
            self::getALL(1);
            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }

    }

    /**
     * 检测当前应用方法名是否重复
     * @param $data
     * @return bool
     */
    /**
     * 检测当前是否重复
     * @param $data
     * @return bool
     */
    private static function chackAction($data){
        if(empty($data['cat_id'])){
            $data['cat_id']=0;
        }

        $count=self::where('cat_key',$data['cat_key'])
            ->where('cat_id',"<>",$data['cat_id'])
            ->count();

        if($count>0){
            return false;
        }else{
            return true;
        }
    }

}